package cn.group7.padserver.controller;

import cn.group7.padserver.common.Flag;
import cn.group7.padserver.controller.responseformat.Code;
import cn.group7.padserver.controller.responseformat.Result;
import cn.group7.padserver.entity.UserInfo;
import cn.group7.padserver.pojo.Bank;
import cn.group7.padserver.pojo.Finance;
import cn.group7.padserver.pojo.User;
import cn.group7.padserver.service.BankService;
import cn.group7.padserver.service.FinanceService;
import cn.group7.padserver.service.UserService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 谷汉斌
 * @description 融资模块
 * @createTime 2024/7/31 上午9:15
 */
@RestController
@RequestMapping("/finances")
@Slf4j
public class FinanceController {

    @Autowired
    private FinanceService financeService;

    @Autowired
    private BankService bankService;

    @Autowired
    private UserService userService;

    /**
     * 添加单人贷款
     *
     * @param finance 添加单人贷款信息
     * @return Result
     */
    @ApiOperation(value = "添加单人贷款")
    @PostMapping("/add")
    public Result add(@RequestBody Finance finance) {
        log.info("添加单人贷款信息：finance{}", finance);
        //获取登陆的用户名
        UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = principal.getUsername();
        User userInfo = userService.selectByUserName(name);
        finance.setOwnName(userInfo.getUserName());
        finance.setPhone(userInfo.getPhone());
        finance.setRealName(userInfo.getRealName());
        finance.setIdNum(userInfo.getIdentityNum());
        finance.setStatus(0);
        Code code = financeService.add(finance) ? Code.ADD_SUCCESS : Code.ADD_FAIL;
        return Result.builder()
                .flag(Flag.SUCCESS.isCode())
                .code(code.getCode())
                .data(null)
                .message(code.getInfo())
                .build();
    }

    /**
     * 查询银行情报
     *
     * @return Result
     */
    @ApiOperation(value = "查询银行情报")
    @GetMapping("/selectBank")
    public Result selectBank() {
        List<Bank> banks = bankService.selectAllBank();
        return Result.builder()
                .flag(Flag.SUCCESS.isCode())
                .code(Code.QUERY_SUCCESS.getCode())
                .data(banks)
                .message(Code.QUERY_SUCCESS.getInfo())
                .build();
    }

    /**
     * 添加组合贷款
     *
     * @param finance
     * @return Result
     */
    @ApiOperation(value = "添加组合贷款")
    @PostMapping("/addFinanceMulti")
    public Result addFinanceMulti(@RequestBody Finance finance) {
        System.out.println(finance);
//        获取登陆的用户名
        UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = principal.getUsername();
        finance.setOwnName(name);
        System.out.println(name);
        Code code = financeService.addMulti(finance) ? Code.ADD_SUCCESS : Code.ADD_FAIL;
        return Result.builder()
                .flag(Flag.SUCCESS.isCode())
                .code(code.getCode())
                .data(null)
                .message(code.getInfo())
                .build();
    }

    /**
     * 查询融资情况
     *
     * @return Result
     */
    @ApiOperation(value = "查询融资情况")
    @GetMapping("/selectByName")
    public Result selectByName() {
        //获取登陆的用户名
        UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = principal.getUsername();
        Finance finance = new Finance();
        finance.setOwnName(name);
        List<Finance> finances = financeService.selectByFinance(finance);
        return Result.builder()
                .flag(Flag.SUCCESS.isCode())
                .code(Code.QUERY_SUCCESS.getCode())
                .data(finances)
                .message(Code.QUERY_SUCCESS.getInfo())
                .build();
    }


    /**
     * 根据id修改融资情报
     *
     * @param finance
     * @return Result
     */
    @ApiOperation(value = "根据id修改融资情报")
    @PutMapping("/update")
    public Result update(@RequestBody Finance finance) {
        log.info("根据id修改融资情报finance{}", finance);
        Code code = financeService.updateByFinance(finance) ? Code.UPDATE_SUCCESS : Code.UPDATE_FAIL;
        return Result.builder()
                .flag(Flag.SUCCESS.isCode())
                .code(code.getCode())
                .message(code.getInfo())
                .build();
    }

    /**
     * 根据id删除融资情报
     *
     * @param id 融资id
     * @return Result
     */
    @ApiOperation(value = "根据id删除融资情报")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        log.info("根据id删除融资情报id{}", id);
        Code code = financeService.deleteByFinanceId(id) ? Code.DELETE_SUCCESS : Code.DELETE_FAIL;
        return Result.builder()
                .flag(Flag.SUCCESS.isCode())
                .code(code.getCode())
                .message(code.getInfo())
                .build();
    }

    /**
     * 根据id查询融资情报
     *
     * @param id 融资id
     * @return Result
     */
    @ApiOperation(value = "根据id查询融资情报")
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable("id") Integer id) {
        Finance finance = financeService.selectByFinanceId(id);
        return Result.builder()
                .flag(Flag.SUCCESS.isCode())
                .code(Code.QUERY_SUCCESS.getCode())
                .data(finance)
                .message(Code.QUERY_SUCCESS.getInfo())
                .build();
    }
}
